﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * Main for exercise.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FileSystem;
using FileSystem.Utility;

namespace FileSystem.Impl
{
	class Program
	{
		private AppResources resources;
	
		private Program(AppResources resources) {
			this.resources = resources;
		}
	
		static void Main(string[] args) {

            Container.Init();
			//Typically this would be done with a Class Loader,
			//but that is beyond scope of exercise.
			//Only needed if this exercise is combined with Prototype Pattern
			ClassLoaderUtil.forceStaticInitialization(); 

			//ConsoleOutUtil.println("Exercise started.");
		
			AppResources resources = AppResources.getInstance();
			Program m = new Program(resources);
			//m.doHelloWorld();
			m.doInputLoop();
		
			ConsoleOutUtil.println("Exercise ended.");
			while (true) ; //NOTE:  This is added to support Visual Studio Express not closing the command prompt window.
		}
	
		public void doHelloWorld() {
			describeParseUtil();
			describeFactory();
			describeFileSystemRoot();
		}
	
		public void describeParseUtil() {
			String text = "The quick brown fox jumps over the fence";
			ConsoleOutUtil.println("Illustrate the string parse utility for string '" + text + "'.");
			ConsoleOutUtil.println("First char : " + StringUtil.firstChar(text));
			ConsoleOutUtil.println("First token: " + StringUtil.firstToken(text));
			ConsoleOutUtil.println("Remainder  : " + StringUtil.remainder(text));
			ConsoleOutUtil.println("Lower      : " + StringUtil.toLower(text));
			ConsoleOutUtil.println("Upper      : " + StringUtil.toUpper(text));
		
			String word = "nix";
			ConsoleOutUtil.println("Illustrate the string parse utility for word '" + word + "'.");
			ConsoleOutUtil.println("Pig Latin  : " + StringUtil.pigLatin(word));
			ConsoleOutUtil.println("Redact     : " + StringUtil.redact(word));
		}
	
		public void describeFactory() {
			ConsoleOutUtil.println("\n\nDescribe the command factory:");
			ICollection<String> names = resources.getCommandFactory().getNames();
			ConsoleOutUtil.println("There are " + names.Count() + " commands registered.");
			int count = 0;
			IEnumerator<String> i = names.GetEnumerator();
			while (i.MoveNext()) {
				ConsoleOutUtil.println("Entry " + count + ": " + i.Current);
				count++;
			}
		}
	
		public void describeFileSystemRoot() {
			ConsoleOutUtil.println("\n\nDescribe all contents of root directory node:");
			IFSNode root = resources.getFileSystem().getRoot();
			ConsoleOutUtil.println(root.describe(new PrintContextImpl()));
		
			ConsoleOutUtil.println("Describe contents of each file:");
			for (int i = 0; i < root.getSize(); i++) {
				ConsoleOutUtil.print(root.getNode(i).describe(new PrintContextImpl()));			
				ConsoleOutUtil.print(root.getNode(i).getContents());
				ConsoleOutUtil.println("");
			}
		}

		private void doInputLoop() {
			//ConsoleOutUtil.println("\n\nStarting Input Loop.");
			IInputGenerator generator = resources.getInput();
			IInputProcessor processor = resources.getProcessor();
			Boolean loop = true;
			while (loop) {
				//Display prompt directly so it does not look like output to tests.
				ConsoleOutUtil.print(processor.getPrompt());
				String input = generator.readInputLineSafe();
				loop = processor.process(input);
			}
		}
	}
}
